using System;
using System.Collections;
using System.ComponentModel;
using PickGold.Charting;

namespace PickGold.Charting.Formulas
{
	internal class FormulaRegistry : IServiceProvider
	{
		private Hashtable _createdModules = new Hashtable(StringComparer.OrdinalIgnoreCase);
		private ArrayList _modulesNames = new ArrayList();
		internal Hashtable registeredModules = new Hashtable(StringComparer.OrdinalIgnoreCase);

		public IFormula GetFormulaModule(string name)
		{
			if (!this.registeredModules.Contains(name))
			{
				throw new ArgumentException(SR.ExceptionFormulaModuleNameUnknown(name));
			}
			if (!this._createdModules.Contains(name))
			{
				this._createdModules[name] = ((Type)this.registeredModules[name]).Assembly.CreateInstance(((Type)this.registeredModules[name]).ToString());
			}
			return (IFormula)this._createdModules[name];
		}

		public string GetModuleName(int index)
		{
			return (string)this._modulesNames[index];
		}

		public void Register(string name, Type moduleType)
		{
			if (this.registeredModules.Contains(name))
			{
				if (this.registeredModules[name].GetType() != moduleType)
				{
					throw new ArgumentException(SR.ExceptionFormulaModuleNameIsNotUnique(name));
				}
			}
			else
			{
				this._modulesNames.Add(name);
				bool flag = false;
				foreach (Type type in moduleType.GetInterfaces())
				{
					if (type == typeof(IFormula))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					throw new ArgumentException(SR.ExceptionFormulaModuleHasNoInterface);
				}
				this.registeredModules[name] = moduleType;
			}
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		object IServiceProvider.GetService(Type serviceType)
		{
			if (serviceType != typeof(FormulaRegistry))
			{
				throw new ArgumentException(SR.ExceptionFormulaModuleRegistryUnsupportedType(serviceType.ToString()));
			}
			return this;
		}

		public int Count
		{
			get
			{
				return this._modulesNames.Count;
			}
		}
	}
}

